#ifndef  _BUF_MAP_H_
#define  _BUF_MAP_H_

#include "../define/define.h"
#include "../trans_server/server_recv.h"
#include "../util/log.h"

struct buf_st
{
   uint32       socket;
   server_recv* send;
};

class buf_map
{
public:
  buf_map()
  {
	  m_log = new logfile("buf_map");
  }

  ~buf_map()
  {
  }

  bool add_map(buf_st  send)
  {

	std::map<uint32,server_recv*>::iterator iter;
	iter = server_list.find(send.socket);
	if(iter != server_list.end())
	{
		char ll[1023] = {0};
		sprintf(ll,"too more socket,socket=%d",send.socket);
		m_log->WriteTrace(ll);
		return false;
	}

	char lls[1024] = {0};
	sprintf(lls,"add_map:end=%x,socket=%d,size=%d",send.send,send.socket,server_list.size());
	m_log->WriteTrace(lls);

	server_list.insert(
		std::map<uint32,server_recv*>::value_type(send.socket,send.send));
	
	return true;
  }

  bool del_map(buf_st  send)
  {
	std::map<uint32,server_recv*>::iterator iter;
	iter = server_list.find(send.socket);
	if(iter == server_list.end())
	{	
		char ll[1023] = {0};
		sprintf(ll,"map not find socket,socket=%d",send.socket);
		m_log->WriteTrace(ll);

		return false;
	}

	char lls[1024] = {0};
	sprintf(lls,"del_map:send=%x,socket=%d,size=%d",send.send,send.socket,server_list.size());
	m_log->WriteTrace(lls);

	
	  server_list.erase(iter);
	  return true;
  }

  int get_map(buf_st* send,uint32 count)
  {
	  std::map<uint32,server_recv*>::iterator iter = server_list.begin();

	  uint32 i=0;
	  while (i< server_list.size() )
	  {
		  if(i == count)
		  {
			  m_log->WriteTrace("send buf is too big");
			  return i;
		  }

		  send[i].socket = iter->first;
		  send[i].send   = iter->second;
		  iter++;
		  i++;
	  }
	 
      return i;
  }

private:
		
   logfile*                             m_log;
   std::map<uint32,server_recv*>        server_list;
 
};

#endif